package com.staryy.activiti7_workflow;

import org.activiti.api.model.shared.model.VariableInstance;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**
 * activiti7
 * 新特性 ProcessRuntime
 * 进行操作前，必须登录Security
 */
@SpringBootTest
public class Part8_ProcessRuntime {

    @Autowired
    private ProcessRuntime processRuntime;
    @Autowired
    private SecurityUtil securityUtil;

    /**
     * 获取流程实例
     */
    @Test
    public void getProcessInstance(){
        // 登录
        securityUtil.logInAs("bajie");
        Page<ProcessInstance> processInstancePage = processRuntime.processInstances(Pageable.of(0, 100));
        System.out.println(processInstancePage.getTotalItems());
        List<ProcessInstance> list = processInstancePage.getContent();
        for(ProcessInstance instance : list){
            System.out.println("--------------");
            System.out.println("id:" + instance.getId());
            System.out.println("name:" + instance.getName());
            System.out.println("StartDate:" + instance.getStartDate());
            System.out.println("Status:" + instance.getStatus());
            System.out.println("ProcessDefinitionId:" + instance.getProcessDefinitionId());
            System.out.println("ProcessDefinitionKe:" + instance.getProcessDefinitionKey());
            System.out.println("BusinessKey:" + instance.getBusinessKey());
        }
    }

    /**
     * 启动流程实例
     */
    @Test
    public void startProcessInstance(){
        securityUtil.logInAs("bajie");
        processRuntime.start(ProcessPayloadBuilder
                .start()
                // 流程定义key
                .withProcessDefinitionKey("")
                // 流程实例名称
                .withName("第一个流程实例名称")
                // 参数
//                .withVariable("","")
                // 业务主键
                .withBusinessKey("bk000001")
                .build());
    }

    /**
     * 删除流程实例
     */
    @Test
    public void delProcessInstance(){
        securityUtil.logInAs("bajie");
        processRuntime.delete(ProcessPayloadBuilder
                .delete()
                .withProcessInstanceId("流程实例ID")
                .build());

    }

    /**
     * 挂起与激活流程实例 (status:RUNNING/SUSPENED)
     */
    @Test
    public void updateProcessIntance(){
        securityUtil.logInAs("bajie");
        // 挂起
        processRuntime.suspend(ProcessPayloadBuilder
                .suspend()
                .withProcessInstanceId("流程实例ID")
                .build());
        // 激活
        processRuntime.resume(ProcessPayloadBuilder
                .resume()
                .withProcessInstanceId("流程实例ID")
                .build());

    }

    /**
     * 查询流程参数
     */
    @Test
    public void getVars(){
        List<VariableInstance> variables = processRuntime.variables(ProcessPayloadBuilder
                .variables()
                .withProcessInstanceId("流程实例ID")
                .build());
    }
}
